package com.yxq.mdt.app.modules.search.model;

import com.yxq.mdt.app.base.RespBase;
import com.yxq.mdt.app.base.ibase.IBaseModel;
import com.yxq.mdt.app.modules.search.bean.SearchBean;
import com.yxq.mdt.app.modules.search.bean.SearchGroupBean;
import com.yxq.mdt.app.modules.search.bean.SearchTopicBean;
import com.yxq.mdt.app.modules.search.bean.SearchUserBean;
import com.yxq.mdt.app.net.ApiModel;
import com.yxq.mdt.app.net.BaseObserver;
import com.yxq.mdt.app.net.RetrofitFactory;

import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.schedulers.Schedulers;

public class SearchModel implements IBaseModel {

    public void search(String keyword, String defaultCat, ApiModel.CommonCallback<SearchBean> callback) {
        RetrofitFactory.getRetrofit().create(SearchApi.class)
                .search(keyword, defaultCat)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<RespBase<SearchBean>>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(RespBase<SearchBean> respBase) {
                        if (respBase.getCode() == 0) {
                            callback.onSuccess(respBase.getData());
                        } else {
                            callback.onError(respBase.getMsg());
                        }
                    }
                });
    }

    public void searchGsUser(String keyword, int pageNum, int pageSize, ApiModel.CommonCallback<SearchUserBean> callback) {
        RetrofitFactory.getJavaRetrofit().create(SearchApi.class)
                .searchGsUser(keyword, pageNum, pageSize)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SearchUserBean>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(SearchUserBean respBase) {

                        callback.onSuccess(respBase);

                    }
                });
    }

    public void searchGroup(String keyword, int pageNum, int pageSize, ApiModel.CommonCallback<SearchGroupBean> callback) {
        RetrofitFactory.getJavaRetrofit().create(SearchApi.class)
                .searchGroup(keyword, pageNum, pageSize)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SearchGroupBean>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(SearchGroupBean respBase) {

                        callback.onSuccess(respBase);

                    }
                });
    }

    public void searchTopic(String keyword, int pageNum, int pageSize, ApiModel.CommonCallback<SearchTopicBean> callback) {
        RetrofitFactory.getJavaRetrofit().create(SearchApi.class)
                .searchTopic(keyword, pageNum, pageSize)
                .subscribeOn(Schedulers.io())
                .unsubscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new BaseObserver<SearchTopicBean>() {
                    @Override
                    public void onFailure(Throwable e, boolean isNetWorkError) {
                        callback.onError(e.getMessage());
                    }

                    @Override
                    public void onSuccess(SearchTopicBean respBase) {

                        callback.onSuccess(respBase);

                    }
                });
    }

    @Override
    public void onDestroy() {

    }
}
